<template>
  <!-- 最外层的容器 -->
  <!-- 最外层的容器 -->
  <view class="my-settle-container">
    <!-- 全选区域 -->
    <label class="radio">
      <radio color="#C00000" @click="chkHandle" :checked="isAllChk" /><text>全选</text>
    </label>

    <!-- 合计区域 -->
    <view class="amount-box">
      合计:<text class="amount">￥{{sumChk}}</text>
    </view>

    <!-- 结算按钮 -->
    <view class="btn-settle" @click="payHandle">结算({{totalChk}})</view>
  </view>
</template>

<script>
  import {
    mapGetters,
    mapMutations,
    mapState
  } from 'vuex'
  export default {
    data() {
      return {}
    },
    computed: {
      ...mapState('cart', ['cart']),
      ...mapState('user', ['token']),
      ...mapGetters('user', ['addstr']),
      ...mapGetters('cart', ['total', 'totalChk', 'sumChk']),
      isAllChk() {
        return this.total === this.totalChk
      }
    },
    methods: {
      ...mapMutations('cart', ['changeAllState']),
      chkHandle() {
        this.changeAllState(!this.isAllChk)
      },
      payHandle() {
        /* 
        1-判断是否登录
          1.1 如果没有登录 跳转到登录页====my
          1.2 如果登录那就 继续走
        2-判断是否有选中商品
        3-判断是否有收货地址
        4-支付流程
        */
        if (!this.addstr) return uni.showToast({
          title: '请选择收货地址',
          icon: 'error'
        })
        if (this.totalChk == 0) return uni.showToast({
          title: '请选择有效商品',
          icon: 'error'
        })
        if (!this.token) {
          uni.showToast()
          return uni.switchTab({
            url: '../../pages/my/my'
          })
        }

        //4-支付流程
        /* 
        4.1 生成支付订单
         4.2 根据请求地址中有my 就需要携带请求头
        4.2 根据订单预支付
        4.3 调用微信支付 【手机【跳转到微信支付页面】和非手机出现一个二维码】
        4.4 校验是否支付成功
        */
        // 4.1 生成支付订单
        this.payOrder()

      },
      async payOrder() {
        // 4.1 生成支付订单
        const orderInfo = {
          // 开发期间，注释掉真实的订单价格，
          // order_price: this.checkedGoodsAmount,
          // 写死订单总价为 1 分钱
          order_price: 0.01,
          consignee_addr: this.addstr,
          goods: this.cart.filter(x => x.goods_state).map(x => ({
            goods_id: x.goods_id,
            goods_number: x.goods_count,
            goods_price: x.goods_price
          }))
        }
        // 1.2 发起请求创建订单
        const {
          data: res
        } = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
        if (res.meta.status !== 200) return uni.$showMsg('生成订单不成功')

        // 4.2 根据订单预支付
        const {
          data: res2
        } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
          order_number: res.message.order_number
        })
        if (res2.meta.status !== 200) return uni.$showMsg(res2.meta.msg)
        // console.log(res2, 888);
         // 4.3 调用微信支付 【手机【跳转到微信支付页面】和非手机出现一个二维码】
         await uni.requestPayment(res2.message.pay)
          // 4.4 校验是否支付成功
          const {
            data: res3
          } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {
            order_number: res.message.order_number
          })
          if (res3.meta.status !== 200) return uni.$showMsg(res3.meta.msg)
      }
    }
  }
</script>

<style lang="scss">
  .my-settle-container {
    /* 底部固定定位 */
    position: fixed;
    bottom: 0;
    left: 0;
    /* 设置宽高和背景色 */
    width: 100%;
    height: 50px;
    background-color: cyan;
  }

  .my-settle-container {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    // 将背景色从 cyan 改为 white
    background-color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 5px;
    font-size: 14px;

    .radio {
      display: flex;
      align-items: center;
    }

    .amount {
      color: #c00000;
    }

    .btn-settle {
      height: 50px;
      min-width: 100px;
      background-color: #c00000;
      color: white;
      line-height: 50px;
      text-align: center;
      padding: 0 10px;
    }
  }
</style>
